package main

import (
	"chat/config"
	_ "chat/config"
	"chat/global"
	_ "chat/internal/dao/pool"
	"chat/internal/router"
	"chat/internal/server"
	"net/http"
	"os"
	"time"
)

func main() {

	// 初始化日志
	err := global.InitLogger()
	if err != nil {
		global.Logger.Fatalf("InitLogger err: %v", err)
		return
	}

	// 检查是否有image文件夹，用于存放用户头像
	_, err = os.Stat(config.GetConfig().App.ImageDir)
	if os.IsNotExist(err) {
		os.Mkdir(config.GetConfig().App.ImageDir, os.ModePerm)
	}

	// 启动websocket服务
	go server.MyServer.Run()

	// 构造并启动http服务
	r := router.GetRouter()
	s := &http.Server{
		Addr:           "0.0.0.0:" + config.GetConfig().Server.HttpPort,
		Handler:        r,
		ReadTimeout:    config.GetConfig().Server.ReadTimeout * time.Second,
		WriteTimeout:   config.GetConfig().Server.WriteTimeout * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	err = s.ListenAndServe()
	if err != nil && err != http.ErrServerClosed {
		global.Logger.Errorf("s.ListenAndServe err: %v", err)
		return
	}
}
